package fylder.recycler.demo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import fylder.recycler.demo.R;

/**
 * 演示,不建议使用
 * <p/>
 * Created by fylder on 15-11-23.
 */
public class DemoAdpater extends RecyclerView.Adapter {

    final int loadingType = 0;
    final int dataType = 1;
    int count = 0;
    int size = 0;
    Context context;

    int stats = 0;
    int REFRESH = 1;
    int LOADING = 2;
    int ENDING = 3;

    public DemoAdpater(Context context) {
        this.context = context;
        stats = REFRESH;
    }

    public void refresh(int c) {
        this.size = c;
        count = size;
        stats = REFRESH;
        notifyDataSetChanged();
    }

    public void loading() {
        count = size;
        stats = LOADING;
        notifyDataSetChanged();
    }

    public void ending(int c) {
        this.size = c;
        stats = ENDING;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        LayoutInflater inflater = LayoutInflater.from(context);
        if (viewType == loadingType) {
            viewHolder = new LoadingViewHolder(inflater.inflate(R.layout.loading_lay, parent, false));
        } else {
            viewHolder = new DemoViewHolder(inflater.inflate(R.layout.item_view, parent, false));
        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (getItemViewType(position) == loadingType) {
            LoadingViewHolder viewHolder = (LoadingViewHolder) holder;
            if (stats == LOADING) {
                viewHolder.lT.setVisibility(View.VISIBLE);
                viewHolder.lT.setText("加载中...");
            } else if (stats == REFRESH) {
                viewHolder.lT.setVisibility(View.INVISIBLE);
            } else {
                viewHolder.lT.setVisibility(View.VISIBLE);
                viewHolder.lT.setText("加载完了");
            }
        }

    }

    @Override
    public int getItemCount() {
        return count + 1;
    }

    @Override
    public int getItemViewType(int position) {

        if (count == 0) {
            return loadingType;
        }
        if (position == count) {
            return loadingType;
        } else {
            return dataType;
        }
    }

    class DemoViewHolder extends RecyclerView.ViewHolder {

        public DemoViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class LoadingViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.loading_text)
        TextView lT;

        public LoadingViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
